/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cjava.system.bean.config;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.cjava.system.model.ApertureAssociate;
import org.cjava.system.model.Benefit;
import org.cjava.system.model.State;
import org.cjava.system.service.interfac.IApertureAssociateService;
import org.cjava.system.service.interfac.IBenefitService;
import org.cjava.system.service.interfac.IStateService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;

/**
 *
 * @author user
 */
@ManagedBean
@SessionScoped
public class BenefitBean implements Serializable{
    private static final long serialVersionUID = 1L;
    @ManagedProperty("#{benefitService}")
    IBenefitService benefitService;
    @ManagedProperty("#{apertureAssociateService}")
    IApertureAssociateService apertureAssociateService;
    @ManagedProperty("#{stateService}")
    IStateService stateService;
    @ManagedProperty("#{clazz}")
    Clazz clazz;
    //////////////////////////////////////
    private Benefit benefit = new Benefit();
    private List<Benefit> listBenefit;
    //////// PARA LA APERTURA DE NUEVOS CARNET'S ////////////
    private ApertureAssociate apertureAssociate = new ApertureAssociate();
    /////////////////////////////////////////////////////////
    private List<State> listStateBenefit;
    
    public void initBalueSystemUser() {
        
    }
    
    public void initValueApertureAssociate() throws Exception {
        
        String HQLL = "from ApertureAssociate a where a.year=" + Calendar.getInstance().get(Calendar.YEAR);
        List<ApertureAssociate> listTempAA = apertureAssociateService.listHQL(HQLL);
        if( listTempAA.isEmpty() ) {
            apertureAssociate = new ApertureAssociate();
            apertureAssociate.setYear(Calendar.getInstance().get(Calendar.YEAR));
        } else {
            apertureAssociate = listTempAA.get(0);
        }
//        String HQL = "select max(a.id) from ApertureAssociate a";
//        List<Integer> myId = apertureAssociateService.listHQL(HQL);
//        System.out.println(myId);
//        if( myId.get(0) == null ){
//            apertureAssociate.setYear(new Date().getYear() + 1900);
//        } else {
//            HQL = "from ApertureAssociate a where a.id=" + myId.get(0);
//            List<ApertureAssociate> listTemp = apertureAssociateService.listHQL(HQL);
//            //VERIFICA SI ES QUE AUN SIGUE SIENDO EL MISMO AÑO
//            if(listTemp.get(0).getYear() == (new Date().getYear() + 1900)) {
//                apertureAssociate = listTemp.get(0);
//            } else {
//                apertureAssociate = new ApertureAssociate();
//            }            
//        }
    }
    
    public void saveOrUpdateApertureAssociate() throws Exception {
        if( apertureAssociate.getId() == null) {
            apertureAssociateService.save(apertureAssociate);
            FacesUtil.addMessage("Guardado con éxito", null, FacesMessage.SEVERITY_INFO);
        } else {
            apertureAssociateService.update(apertureAssociate);
            FacesUtil.addMessage("Información Actualizada", null, FacesMessage.SEVERITY_INFO);
        }
    }
    
    public void initValues() throws Exception {
        benefit = new Benefit();
        listBenefit = null;
        getListBenefit();
    }
    
    public void saveOrUpdateBenefit() throws Exception {
        if( benefit.getId() == null) {
            benefitService.save(benefit);
            listBenefit.add(benefit);
            benefit = new Benefit();
            FacesUtil.addMessage("Guardado con éxito...", null, FacesMessage.SEVERITY_INFO);
        } else {
            benefitService.update(benefit);
            listBenefit = null;
            benefit = new Benefit();
            FacesUtil.addMessage("Actualizado con éxito...", null, FacesMessage.SEVERITY_INFO);
        }
    }
    
    public void deleteBenefit(Benefit benefitOfTable) throws Exception {
        benefitService.delete(benefitOfTable);
        listBenefit = null;
    }
    
    public Benefit getBenefit() {
        return benefit;
    }

    public void setBenefit(Benefit benefit) {
        this.benefit = benefit;
    }

    public List<Benefit> getListBenefit() throws Exception {
        if( listBenefit == null ) {
            listBenefit = benefitService.list();
        }
            
        return listBenefit;
    }

    public void setListBenefit(List<Benefit> listBenefit) {
        this.listBenefit = listBenefit;
    }

    public void setBenefitService(IBenefitService benefitService) {
        this.benefitService = benefitService;
    }

    public void setApertureAssociateService(IApertureAssociateService apertureAssociateService) {
        this.apertureAssociateService = apertureAssociateService;
    }

    public ApertureAssociate getApertureAssociate() {
        return apertureAssociate;
    }

    public void setApertureAssociate(ApertureAssociate apertureAssociate) {
        this.apertureAssociate = apertureAssociate;
    }

    public void setStateService(IStateService stateService) {
        this.stateService = stateService;
    }

    public List<State> getListStateBenefit() throws Exception {
        if( listStateBenefit == null ) {
            listStateBenefit = stateService.listHQL("from State s where s.entityName='Benefit'");
            if(listStateBenefit.isEmpty()) {
                listStateBenefit.add(clazz.STATE(Clazz._STATE._NAME._BENEFIT_ACTIVO, Clazz._STATE._ENTITY._Benefit));
                listStateBenefit.add(clazz.STATE(Clazz._STATE._NAME._BENEFIT_FINALIZADO, Clazz._STATE._ENTITY._Benefit));
            }   
        }
        return listStateBenefit;
    }

    public void setListStateBenefit(List<State> listStateBenefit) {
        this.listStateBenefit = listStateBenefit;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }
    
    
}
